package com.gobo.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gobo.commonutils.R;
import com.gobo.eduservice.pojo.EduCourse;
import com.gobo.eduservice.pojo.EduTeacher;
import com.gobo.eduservice.pojo.vo.CourseInfo;
import com.gobo.eduservice.pojo.vo.CoursePublishVo;
import com.gobo.eduservice.pojo.vo.EduCourseQuery;
import com.gobo.eduservice.pojo.vo.EduTeacherQuery;
import com.gobo.eduservice.service.EduCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author gobo-generator
 * @since 2021-09-19
 */
@RestController
@RequestMapping("/eduservice/course")
public class EduCourseController {

    @Autowired
    EduCourseService courseService;

    /**
     *
     * @param current 当前页码数
     * @param size 每页显示数据条数
     * @return R
     */
    @GetMapping("findPage/{current}/{size}")
    public R fingPage(@PathVariable Long current,
                      @PathVariable Long size){

        Page<EduCourse> pageParam = new Page<>(current,size);
        courseService.page(pageParam,null);
        long total = pageParam.getTotal();
        List<EduCourse> records = pageParam.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }

    /**
     * 条件组合查询所有课程信息
     * @param current 当前页数
     * @param size 每页数据数
     * @param eduCourseQuery 搜寻条件的vo对象
     * @return R
     */
    @PostMapping("findByPage/{current}/{size}")
    public R pageQuery(@PathVariable  Long current,
                       @PathVariable Long size,
                       @RequestBody EduCourseQuery eduCourseQuery){
        if (eduCourseQuery==null){
            return this.fingPage(current,size);
        }
        Page<EduCourse> pageParam = new Page<>(current,size);
        courseService.pageQuery(pageParam,eduCourseQuery);
        return R.ok().data("total",pageParam.getTotal()).data("rows",pageParam.getRecords());
    }

    @PostMapping("/save")
    public R saveCourseAndDescription(@RequestBody CourseInfo courseInfo){
        String id = courseService.saveCourseAndDescription(courseInfo);
        return id==null ? R.error():R.ok().data("id",id);
    }
    @GetMapping("/getCourseInfoById/{courseId}")
    public R getCourseInfoById(@PathVariable  String courseId){
        CourseInfo courseInfo = courseService.getCourseInfoById(courseId);
        return courseInfo==null ? R.error():R.ok().data("item",courseInfo);
    }
    @PostMapping("/update")
    public R updateCourseInfoById(@RequestBody CourseInfo courseInfo){
        String id = courseService.updateCourseInfoById(courseInfo);
        return id==null ? R.error():R.ok().data("id",id);
    }
    @GetMapping("/getCoursePublishById/{courseId}")
    public R getCoursePublishById(@PathVariable  String courseId){
        CoursePublishVo coursePublishVo = courseService.getCoursePublishVo(courseId);
        return coursePublishVo==null ? R.error():R.ok().data("item",coursePublishVo);
    }

    /**
     * 若已发布=>更改未发布
     * 未发布=>已发布
     * @param course
     * @return
     */
    @PostMapping("/updateStatus")
    public R updateCoursePublishedById(@RequestBody EduCourse course){
        course.setStatus("Normal");
        boolean id = courseService.updateById(course);
        return id ? R.ok():R.error();
    }
    @GetMapping("/updateToPublish/{courseId}")
    public R updateToPublish(@PathVariable String courseId){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setStatus("Normal");
        eduCourse.setId(courseId);
        boolean istrue = courseService.updateById(eduCourse);
        return istrue ? R.ok():R.error();
    }

    /**
     * 删除对应课程所有记录(章节小节视频以及课程描述课程详情)
     * @param courseId
     * @return
     */
    @DeleteMapping("/delete/{courseId}")
    public R deleteCourseByCourseId(@PathVariable String courseId){
        Boolean result = courseService.removeCourseByCourseId(courseId);
        return result ? R.ok():R.error();
    }

}

